package com.tz.crm.controller.interceptor;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*
*
 * 用户登录状态拦截器
 * Created by hjl on 2016/11/16.
*/


@Component
public class SessionLoginInterceptor extends HandlerInterceptorAdapter {

    private static final Logger LOGGER = Logger.getLogger(SessionLoginInterceptor.class);

/*
*
     * 空参构造

*/

    public SessionLoginInterceptor() {
        LOGGER.debug("SessionLoginInterceptor empty constructor...");
    }

/**
     * 拦截方法
     * 根据用户是否已登录来决定是否放行请求
     *
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     * @param handler  Object
     * @return view
     * @throws Exception 异常
     */

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
//        HttpSession session = request.getSession();
//        User user = (User) session.getAttribute(IConst.LOGIN_USER);
//        if (user == null) {
//            //用户未登录，跳转至登录页面，传递数据
//            LOGGER.warn("发现未登录的用户!");
//            session.setAttribute(IConst.LOGIN_ERROR_MESSAGE, "用户尚未登录!");
//            ModelAndView mav = new ModelAndView("redirect:/initLogin.do");
//            throw new ModelAndViewDefiningException(mav);
//        } else {
//            //已登录
//            return true;
//        }
        return true;
    }
}
